﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Biomedica.Nui.ui
{
    public class UserControlTransition
    {
        /// <summary>
        /// This static function will Perform slide animation on A FrameWork Element
        /// </summary>
        /// <param name="elem">The Element to animate</param>
        /// <param name="x">Starting point dpends on paraperer hv</param>
        /// <param name="y">Ending Point depend on Parameter hv</param>
        /// <param name="hv">if hv 0 transtion will be on the Y Axis if the hv is 1 it will be on the X Axis</param>
        /// <param name="time">Duration</param>
        /// <param name="OnAnimationComplet">Call back function when the transition is complete</param>
        public static void DoControlTransitionAnimation(FrameworkElement elem, Double x, Double y, int hv,double time, EventHandler OnAnimationComplet)
        {
           
            DoubleAnimation da = new DoubleAnimation
              (x, y, new Duration(TimeSpan.FromMilliseconds(time)));
            if (OnAnimationComplet != null) da.Completed += OnAnimationComplet;
            TranslateTransform rt = new TranslateTransform();
         

            elem.RenderTransform = rt;
            if(hv==0)
            rt.BeginAnimation(TranslateTransform.XProperty, da);
            else if (hv == 1)
                rt.BeginAnimation(TranslateTransform.YProperty, da);
            else
            {
                rt.BeginAnimation(TranslateTransform.XProperty, da);
                rt.BeginAnimation(TranslateTransform.YProperty, da);
            }
        }

        public static void DoControlScaleAnimation(FrameworkElement elem, Double x, Double y, int hv, double time, EventHandler OnAnimationComplet)
        {
            DoubleAnimation da = new DoubleAnimation
             (x, y, new Duration(TimeSpan.FromMilliseconds(time)));
            if (OnAnimationComplet != null) da.Completed += OnAnimationComplet;
            ScaleTransform st = new ScaleTransform();
            elem.RenderTransform = st;
            if (hv == 0)
                st.BeginAnimation(ScaleTransform.ScaleXProperty, da);
            else if (hv == 1)
                st.BeginAnimation(ScaleTransform.ScaleYProperty, da);
            else
            {
                st.BeginAnimation(ScaleTransform.ScaleXProperty, da);
                st.BeginAnimation(ScaleTransform.ScaleYProperty, da);
            }
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }
    }
}
